//
// Created by 17775 on 2024/10/11.
//

#include "Single_Measurement.h"

Single_Measurement::Single_Measurement(int & docId) : _iSingleDocId(docId) {

}

bool Single_Measurement::isRecordingAvailable() {
    bool bRecodingAvailable = false;
    RayCi_Single_Recording_isAvailable(_iSingleDocId, &bRecodingAvailable);
    return bRecodingAvailable;
}

bool Single_Measurement::isRecording() {
    bool bRecoding = false;
    RayCi_Single_Recording_isRecording(_iSingleDocId, &bRecoding);
    return bRecoding;
}

bool Single_Measurement::isFrameSpan() {
    bool bFrameSpan = false;
    RayCi_Single_Recording_Settings_isFrameSpan(_iSingleDocId, &bFrameSpan);
    return bFrameSpan;
}

void Single_Measurement::setFrameSpan(int frames) {
    RayCi_Single_Recording_Settings_setFrameSpan(_iSingleDocId, frames);
}

int Single_Measurement::getFrameSpan() {
    int frameSpan = 0;
    RayCi_Single_Recording_Settings_getFrameSpan(_iSingleDocId, &frameSpan);
    return frameSpan;
}

int Single_Measurement::getFrameStepCount() {
    int frameStepCount = 0;
    RayCi_Single_Recording_Settings_getFrameStepCount(_iSingleDocId, &frameStepCount);
    return frameStepCount;
}

double Single_Measurement::getTimeSpan() {
    double timeSpan = 0;
    RayCi_Single_Recording_Settings_getTimeSpan(_iSingleDocId, &timeSpan);
    return timeSpan;
}

void Single_Measurement::setTimeSpan(double dTime) {
    RayCi_Single_Recording_Settings_setTimeSpan(_iSingleDocId, dTime);
}

bool Single_Measurement::isMedianAvailable() {
    bool bMedianAvailable = false;
    RayCi_Single_Recording_Settings_isMedianAvailable(_iSingleDocId, &bMedianAvailable);
    return bMedianAvailable;
}

void Single_Measurement::setMedianFilter(bool bMedian) {
    bool bMedianAvailable = false;
    RayCi_Single_Recording_Settings_isMedianAvailable(_iSingleDocId, &bMedianAvailable);
    if(bMedianAvailable) {
        RayCi_Single_Recording_Settings_setMedian(_iSingleDocId, bMedian);
    }
}

bool Single_Measurement::isSingleShot() {
    bool bSingleShot = false;
    RayCi_Single_Recording_Settings_isSingleShot(_iSingleDocId, &bSingleShot);
    return bSingleShot;
}

bool Single_Measurement::isSingleShotAvailable() {
    bool bSingleShotAvailable = false;
    RayCi_Single_Recording_Settings_isSingleShotAvailable(_iSingleDocId, &bSingleShotAvailable);
    return bSingleShotAvailable;
}

void Single_Measurement::setSingleShot(bool bSingleShot) {
    RayCi_Single_Recording_Settings_setSingleShot(_iSingleDocId, bSingleShot);
}

void Single_Measurement::start() {
    RayCi_Single_Recording_start(_iSingleDocId);
}

bool Single_Measurement::isLinkStartAvailable() {
    bool bLinkStartAvailable = false;
    RayCi_Single_Recording_Settings_isLinkStartAvailable(_iSingleDocId, &bLinkStartAvailable);
    return bLinkStartAvailable;
}

bool Single_Measurement::isStartLinked() {
    bool bStartLinked = false;
    RayCi_Single_Recording_Settings_isStartLinked(_iSingleDocId, &bStartLinked);
    return bStartLinked;
}

void Single_Measurement::setLinkStart(bool bLink) {
    RayCi_Single_Recording_Settings_setLinkStart(_iSingleDocId, bLink);
}

void Single_Measurement::stop() {
    RayCi_Single_Recording_stop(_iSingleDocId);
}

bool Single_Measurement::isLinkStopAvailable() {
    bool bLinkStopAvailable = false;
    RayCi_Single_Recording_Settings_isLinkStartAvailable(_iSingleDocId, &bLinkStopAvailable);
    return bLinkStopAvailable;
}

bool Single_Measurement::isStopLinked() {
    bool bStopLinked = false;
    RayCi_Single_Recording_Settings_isStopLinked(_iSingleDocId, &bStopLinked);
    return bStopLinked;
}

void Single_Measurement::setLinkStop(bool bLink) {
    RayCi_Single_Recording_Settings_setLinkStop(_iSingleDocId, bLink);
}

void Single_Measurement::clear() {
    RayCi_Single_Recording_clear(_iSingleDocId);
}

double Single_Measurement::getSingleWavelength() {
    double waveLength = 0;
    RayCi_Single_Header_Laser_getWaveLength(_iSingleDocId, &waveLength);
    return waveLength;
}

QString Single_Measurement::getSingleModel() {
    char szModel_buffer[50];
    int nSize = _countof(szModel_buffer);
    RayCi_Single_Header_Laser_getModelA(_iSingleDocId, szModel_buffer, &nSize);
    return QString::fromUtf8(szModel_buffer);
}

QString Single_Measurement::getRemark() {
    char szRemark_buffer[50];
    int nSize = _countof(szRemark_buffer);
    RayCi_Single_Recording_getRemarkA(_iSingleDocId, szRemark_buffer, &nSize);
    return QString::fromUtf8(szRemark_buffer);
}

void Single_Measurement::exportReport() const {
    RayCi_createDirectoryA("C:\\RayCi\\Single");
    OPENFILENAMEA ofn;
    char szFile[260] = { 0 };
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = nullptr;
    ofn.lpstrFile = szFile;
    ofn.nMaxFile = sizeof(szFile) / sizeof(szFile[0]) - 1;
    ofn.lpstrFilter = "PDF Files(*.pdf)\0*.pdf*\0All Files(*.*)\0*.*\0\0";
    ofn.nFilterIndex = 1;
    ofn.lpstrInitialDir = "C:\\RayCi\\Single";
    ofn.lpstrTitle = "Save Report As";
    ofn.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
    if (GetSaveFileNameA(&ofn) == TRUE) {
        const char* reportPathName = ofn.lpstrFile;
        std::string reportPathWithExtension = std::string(reportPathName) + ".pdf";
        RayCi_Single_Report_exportA(_iSingleDocId, reportPathWithExtension.c_str());
        ShellExecuteA(nullptr, "open", reportPathWithExtension.c_str(), nullptr, nullptr, SW_SHOWNORMAL);
    } else {
        MessageBoxA(nullptr, "Export was cancelled.", "Info", MB_OK | MB_ICONINFORMATION);
    }
}
